﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MovieStorage;

namespace DVDFilmoteka.Sorting
{
    class DirectorSorter : ISorter
    {
        private readonly List<String> expanded = new List<string>();

        #region ISorter Members

        public void SaveExpanded(TreeView treeView)
        {
            expanded.Clear();
            foreach (TreeNode item in treeView.Nodes)
            {
                if (item.IsExpanded)
                {
                    expanded.Add(item.Name);
                }
            }
        }

        public void Sort(TreeView treeView, List<MovieDataStore12> movieList)
        {
            // Smazat
            treeView.Nodes.Clear();
            
            // Vytvorit seznam uzlu
            Boolean neznamyExists = false;
            List<String> directors = new List<string>();
            foreach (var item in movieList)
            {
                String director = item.directorName;
                if (String.IsNullOrEmpty(director))
                {
                    neznamyExists = true;
                    continue;
                }

                // Prehodit jmeno a prijmeni
                if (director.Contains(" "))
                {
                    director = director.Replace(" - ", "-");
                    director = director.Replace(" st.", ",st.");
                    director = director.Replace(" ml.", ",ml.");

                    List<String> names = new List<string>(director.Split(' '));
                    String last = names[names.Count - 1];
                    names.RemoveAt(names.Count - 1);

                    director = last + " " + String.Join(" ", names.ToArray());
                }

                // Reziser jeste neni v seznamu uzlu
                if (!directors.Contains(director))
                {
                    directors.Add(director);                    
                }
            }

            directors.Sort();
            directors.Reverse();

            if (neznamyExists) treeView.Nodes.Add("Neznámý", "Neznámý");
            foreach (String item in directors)
            {
                treeView.Nodes.Add(item, item);
            }

            // Naplnit uzli nazvy filmu
            foreach (var item in movieList)
            {
                String director = item.directorName;
                if (String.IsNullOrEmpty(director)) director = "Neznámý";

                // Prehodit jmeno a prijmeni
                if (director.Contains(" "))
                {
                    director = director.Replace(" - ", "-");
                    director = director.Replace(" st.", ",st.");
                    director = director.Replace(" ml.", ",ml.");

                    List<String> names = new List<string>(director.Split(' '));
                    String last = names[names.Count - 1];
                    names.RemoveAt(names.Count - 1);

                    director = last + " " + String.Join(" ", names.ToArray());
                }

                String name = Properties.Messages.messages_nomoviename;
                if (!string.IsNullOrEmpty(item.czechName))
                {
                    name = item.czechName;
                }
                else if (!string.IsNullOrEmpty(item.originalName))
                {
                    name = item.originalName;
                }

                int index = treeView.Nodes.IndexOfKey(director);

                // Reziser neni v seznamu uzlu
                if (index < 0) continue;
                
                TreeNode node = new TreeNode(name);
                node.Tag = item; // V treelistu si drzim ukazatel na data o filmu

                treeView.Nodes[index].Nodes.Add(node);
            }

            // Expanduje korenove nody pokud byli predtim expandovane
            if (expanded != null && expanded.Count > 0)
            {   
                foreach (String item in expanded)
                {
                    if (treeView.Nodes[item] != null)
                        treeView.Nodes[item].Expand();
                }
                treeView.ResumeLayout();
            }
        }

        public ESorters Category()
        {
            return ESorters.DIRECTOR;
        }

        public string Name()
        {
            return Properties.Messages.sorter_DIRECTOR;
        }

        #endregion
    }
}
